
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userId: wx.getStorageSync('userId'),
    })
    this.getUser();
  },
  getUser: function () {//读取会员
    var that = this;
    wx.request({
      url: app.globalData.apiUrl + 'user/getUser',
      data: {
        // opt: 'getUser',
        userId: that.data.userId,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res, '用户信息')
        if (res.data.is_login == 1) {
          that.setData({
            user: res.data,


          })

        } else {
          that.setData({
            canIUse: wx.canIUse('button.open-type.getUserInfo'),
          })
        }
      }

    })
    console.log(that.data.canIUse)
  },
  formSubmit: function (e) {
    console.log(e)

    var that = this;
    var formData = e.detail.value;
    if (formData['Mobile'] == "") {
      wx.showToast({
        title: '必填',
        icon: 'none'
      })
      return;
    }
    if (Number(formData['yzcode']) !== that.data.yanzhengma) {
      wx.showToast({
        title: '验证码不正确',
        icon: 'none'
      })
      return;
    }
    wx.request({
      url: app.globalData.apiUrl + 'user/edit_phone',
      data: formData,
      header: {
        'Content-Type': 'application/json'
      },
      method: 'POST',
      success: function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
          that.onLoad();
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  blurPhone: function (e) {
    console.log(e)
    var phone = e.detail.value;
    let that = this
    if (!(/^1[34578]\d{9}$/.test(phone))) {

      this.setData({
        ajxtrue: false
      })
      // if (phone.length > 11) {
      //   wx.showToast({
      //     title: '手机号有误',
      //     icon: 'success',
      //     duration: 2000
      //   })
      // }
    } else {
      this.setData({
        ajxtrue: true,
        phone: phone,
      })

    }
    console.log('验证成功', that.data.ajxtrue)
  },
  //获取验证码
  get_yanzheng: function () {
    var that = this;
    if (that.data.phone == "" || that.data.ajxtrue == false) {
      wx.showToast({
        title: '手机号有误',
        icon: 'success',
        duration: 2000
      })
      return;
    } else {
      var n = 60;
      that.setData({
        is_send: 1,
        wait_time: n,

      })
      var flag = true;

      var current = 60;
      var timer = null;
      timer = setInterval(function () {
        n--;
        that.setData({
          wait_time: n,
        })
        if (n <= 0) {
          clearInterval(timer);
          n = current;
          var nowflag = true;
          var flag = false;
          that.setData({
            is_send: 0,
            // gety_but: false
          })
        }
      }, 1000);

      wx.request({
        url: app.globalData.apiUrl + 'User/getYzm',
        data: {
          // opt: 'getBanner',
          phone: that.data.phone
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)

          if (res.data.status == 1) {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
            that.setData({
              yanzhengma: res.data.code,
            })

          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none'
            })
          }
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})